package com.bawei.mall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.mall.product.domain.*;
import com.bawei.mall.product.domain.model.MallUserCollectInfo;
import com.bawei.mall.product.domain.model.MallUserFollow;
import com.bawei.mall.product.domain.model.UserFollowList;
import com.bawei.mall.product.domain.request.ProductList;
import com.bawei.mall.product.domain.request.UserCollectList;
import com.bawei.mall.product.service.MallProductService;
import com.bawei.mall.user.domain.MallUserFootprint;
import com.bawei.mall.user.domain.UserList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：张余庆
 * @Package：com.bawei.mall.product.controller
 * @Project：mall_cloud
 * @name：MallProductContorller
 * @Date：2024/6/21 11:57
 */
@RestController
@RequestMapping("/product")
@Api(tags = "新框架商品品牌管理")
public class MallNewProductBrandContorller extends BaseController {
    @Autowired
    private MallProductService service;

    /**
     * 功能：新框架 查询商品品牌
     */
    @GetMapping("/selBrandList")
    @ApiOperation(value = "查询商品品牌列表")
    public R<MallList> selBrandList(){
        MallList list = service.selBrandList();
        return R.ok(list);
    }

    @GetMapping("/selProduct")
    @ApiOperation(value = "根据品牌id查询商品")
    public R<MallList> selProduct(
            @RequestParam("brandId") Integer brandId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        MallList list = service.selProduct(brandId, pageNum, pageSize);
        return R.ok(list);
    }

    /**
     * 功能：根据ID查询商品 和 品牌
     * @param id
     * @return
     */
    @GetMapping("selProductById/{id}")
    @ApiOperation("根据ID查询商品和品牌")
    public R<ProductList> selProductById(@PathVariable("id") Integer id){
        ProductList list = service.selProductById(id);
        return R.ok(list);
    }

    /**
     * 功能：根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("selBrandById/{id}")
    @ApiOperation(value = "根据id查询品牌")
    public R selBrandById(@PathVariable("id") Integer id){
        ProductBrand list = service.selBrandById(id);
        return R.ok(list);
    }

    /**
     * 功能：商品推荐广告查询
     */
    @GetMapping("/selGaoList")
    @ApiOperation(value = "商品推荐广告查询")
    public R<MallList> selGaoList(){
        MallList list = service.selGaoList();
        return R.ok(list);
    }

    /**
     * 功能：查询新品
     * @return
     */
    @GetMapping("selNewProduct")
    @ApiOperation(value = "查询新品")
    public R selNewProduct(){
        List<MallNewProduct> newProduct = service.selNewProduct();
        return R.ok(newProduct);
    }

    /**
     * 功能：查询推荐商品
     * @return
     */
    @GetMapping("selCommentProduct")
    @ApiOperation(value = "查询推荐商品")
    public R selCommentProduct(){
        List<MallNewProduct> commentProduct = service.selCommentProduct();
        return R.ok(commentProduct);
    }

    /**
     * 功能：商品收藏
     * @param mallUserFollow
     * @return
     */
    @PostMapping("add")
    @ApiOperation(value = "收藏商品")
    public R add(@RequestBody MallUserFollow mallUserFollow){
        service.add(mallUserFollow);
        return R.ok();
    }

    /**
     * 功能：取消商品收藏
     * @return
     */
    @PostMapping("delete")
    @ApiOperation(value = "取消商品收藏")
    public R delete(Integer brandId){
        service.delete(brandId);
        return R.ok();
    }

    /**
     * 功能：清空用户关注
     * @return
     */
    @PostMapping("clear")
    @ApiOperation("清空用户关注")
    public R clear(){
        service.clear();
        return R.ok();
    }

    /**
     * 功能：查询用户地址
     * @return
     */
    @GetMapping("userAddress")
    @ApiOperation(value = "查询用户地址")
    public R userAddress(){
        List<MallUserAddress> list = service.userAddress();
        return R.ok(list);
    }

    /**
     * 功能：用户地址回显
     */
    @GetMapping("address/{id}")
    @ApiOperation(value = "用户地址回显")
    public R address(@PathVariable("id") Integer id){
        MallUserAddress list = service.address(id);
        return R.ok(list);
    }

    /**
     * 功能：编辑用户地址
     */
    @PostMapping("updateAddress")
    @ApiOperation(value = "编辑用户地址")
    public R updateAddress(@RequestBody MallUserAddress address){
        service.updateAddress(address);
        return R.ok();
    }

    /**
     * 功能：删除用户地址
     * @param id
     * @return
     */
    @PostMapping("delAddress/{id}")
    @ApiOperation(value = "删除用户地址")
    public R delAddress(@PathVariable("id") Integer id){
        service.delAddress(id);
        return R.ok();
    }

    /**
     * 功能：添加用户地址
     * @param address
     * @return
     */
    @PostMapping("addAddress")
    @ApiOperation(value = "添加用户地址")
    public R add(@RequestBody MallUserAddress address){
        service.addAddress(address);
        return R.ok();
    }

    /**
     * 功能：添加广告推荐表
     */
    @PostMapping("addAdvertise")
    @ApiOperation(value = "添加广告表")
    public R addAdvertise(@RequestBody MallAdvertiseInfo advertiseInfo){
        service.addAdvertise(advertiseInfo);
        return R.ok();
    }

    /**
     * 功能：修改广告推荐表
     */
    @PostMapping("updateAdvertise")
    @ApiOperation(value = "修改广告表")
    public R updateAdvertise(@RequestBody MallAdvertiseInfo advertiseInfo){
        service.updateAdvertise(advertiseInfo);
        return R.ok();
    }

    /**
     * 功能：删除广告推荐表
     * @param id
     * @return
     */
    @GetMapping("delAdvertise/{id}")
    @ApiOperation(value = "删除广告")
    public R delAdvertise(@PathVariable("id") Integer id){
        service.delAdvertise(id);
        return R.ok();
    }

    /**
     * 功能：查询广告
     * @param id
     * @return
     */
    @GetMapping("selAdvertise/{id}")
    @ApiOperation(value = "查询广告")
    public R selAdvertise(@PathVariable("id") Integer id){
        MallAdvertiseInfo advertiseInfo = service.selAdvertise(id);
        return R.ok(advertiseInfo);
    }

    /**
     * 功能：查询用户足迹表
     */
    @GetMapping("/list")
    @ApiOperation("查询用户足迹表")
    public R<UserList> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        UserList list = service.list(pageNum,pageSize);
        return R.ok(list);
    }

    /**
     * 功能：删除用户足迹
     * @return
     */
    @GetMapping("delFootprint")
    @ApiOperation("删除用户足迹")
    public R delFootprint(){
        service.delFootprint();
        return R.ok();
    }

    /**
     * 功能：添加用户足迹表
     * @param mallUserFootprint
     * @return
     */
    @PostMapping("addFootprint")
    @ApiOperation(value = "添加用户足迹表")
    public R addFootprint(@RequestBody MallUserFootprint mallUserFootprint){
        service.addFootprint(mallUserFootprint);
        return R.ok();
    }

    /**
     * 自动识别地址
     * @param address
     * @return
     */
    @GetMapping("/portAddressAnalysis")
    @ApiOperation(value = "自动识别地址")
    public R address(String address){
        if(true){
            Map map = JSONObject.parseObject(address,Map.class);
            return R.ok(map.get("Date"));
        }
        //return R.ok(addressService.address(address));
        return null;
    }

    /**
     * 功能：根据关键字查询商品
     * @return
     */
    @GetMapping("/selProductList")
    @ApiOperation(value = "根据关键字查询商品")
    public R<MallList> selProductList(@RequestParam(value = "pageNum") Integer pageNum,
                                      @RequestParam(value = "pageSize") Integer pageSize,
                                      @RequestParam(value = "keywords") String keywords){
        MallList list = service.selProductList(keywords,pageNum,pageSize);
        return R.ok(list);
    }


    /**
     * 功能：查询用户关注表
     * @return
     */
    @GetMapping("getUserFollow")
    @ApiOperation("查询用户关注表")
    public R<UserFollowList> getUserFollow(@RequestParam(value = "pageNum") Integer pageNum,
                                           @RequestParam(value = "pageSize") Integer pageSize){
        UserFollowList list = service.getUserFollow(pageNum,pageSize);
        return R.ok(list);
    }


    public static void main(String[] args) {
        String used = "1237100";//仅作为示例ID，不可用来实际使用
        //加密私钥，由快递鸟提供
        String keyValue = "25373386-b531-4b13-9b2a-4b3727884a55";//仅作为示例Key，不可用来实际使用
        //请求地址
        String url = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
        //2-json
        String DataType = "2";
        //字符编码采用UTF-8
        String charset = "UTF-8";
        //JSON字符串String
        //        String jsonStr = "{"OrderCode":"","ShipperCode":"YTO","LogisticCode":"YT2253890514882"}";
        Map param= new HashMap();
        param.put("Content","北京市房山区长阳镇怡和北路27号院1号楼803 董卓15227324503");
        String sparmStr = JSONObject.toJSONString(param);
        sparmStr += keyValue;
        sparmStr = encryptMD5(sparmStr);
        sparmStr = encodeToBse64(sparmStr);
        System.out.println(sparmStr);
        //        datasign = HttpUtility.UrlEncode(base64(MD5(sparmStr, "UTF-8"), "UTF-8"), "UTF8");

        //把(jsonStr+APIKey)进行MD5加密，然后Base64编码，最后 进行URL(utf-8)编码
        //        datasign = HttpUtility.UrlEncode(base64(MD5(jsonStr + keyValue, "UTF-8"), "UTF-8"), Encoding.UTF8);
        //请求报文参数
        String PostStr = "RequestType=8001&EBusinessID= used &RequestData=jsonStr &DataSign= datasign&DataType=DataType";
        //通讯协议使用Https协议Post请求方式
        //        String post = DoPost(url, PostStr);
    }
    public static String encodeToBse64(String input){
        byte[] encodeBytes = Base64.getEncoder().encode(input.getBytes());
        return new String(encodeBytes);
    }


    public static String encryptMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());

            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}
